package demo1;

import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;

class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;
    public TreeNode(int val) {
        this.val = val;
    }
}

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int[] inorder = new int[n];
        int[] preorder = new int[n];
        for(int i = 0; i<n; i++) {
            inorder[i] = scanner.nextInt();
        }
        for(int i = 0; i<n; i++) {
            preorder[i] = scanner.nextInt();
        }

        preindex = 0;
        TreeNode root = CreateTree(preorder,inorder,0,n-1);

        reverse(root);
        leveOrder(root);
    }

    public static void reverse(TreeNode root) {
        if(root == null) return;
        if(root.left == null && root.right == null) return;
        TreeNode tmp = root.left;
        root.left = root.right;
        root.right = tmp;
        reverse(root.left);
        reverse(root.right);
    }

    public static void leveOrder(TreeNode root) {
        Queue<TreeNode> queue = new LinkedList<>();
        queue.add(root);
        while(!queue.isEmpty()) {
            TreeNode tmp = queue.poll();
            if(tmp.left!=null) queue.add(tmp.left);
            if(tmp.right!=null) queue.add(tmp.right);
            System.out.print(tmp.val);
            if(!queue.isEmpty()) System.out.print(" ");
        }
    }

    static int preindex;
    public static TreeNode CreateTree(int[] preorder, int[] inorder, int begin, int end) {
        if(begin > end) return null;
        int index = findIndex(inorder,preorder[preindex],begin,end);
        preindex++;
        TreeNode root = new TreeNode(inorder[index]);
        root.left = CreateTree(preorder,inorder,begin,index-1);
        root.right = CreateTree(preorder,inorder,index+1, end);
        return root;
    }
    public static int findIndex(int[] inorder, int target, int begin , int end) {
        for(int i = begin; i<=end; i++) {
            if(inorder[i] == target) {
                return i;
            }
        }
        return -1;
    }
}
